import java.util.*;

import static java.lang.Math.abs;

public class Test {
    public static void main(String[] args){
        HashMap<String, String> map = new HashMap<>();
        int[] array = {1,2,3,1,2,3};
        boolean flag = containsNearbyDuplicate(array, 2);
        System.out.println(flag);
    }

    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            if(!hashMap.containsKey(nums[i])){
                hashMap.put(nums[i], i);
            }else{
                int prev = hashMap.get(nums[i]);
                if(Math.abs(prev - i) <= k){
                    return true;
                }
            }
        }
        return false;
    }
    //存在重复元素I
    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0; i < nums.length; i++){
            if(set.contains(nums[i])){
                return true;
            }
            set.add(nums[i]);
        }
        return false;
    }
}
